//
//  SEChatInquiryInfoHeader.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/27.
//

import UIKit

let kChatInquiryInfoHeaderDefaultHeight: CGFloat = 140
let kChatInquiryInfoHeaderCollectionDefaultHeight: CGFloat = 90
let kChatInquiryInfoHeaderDescLabelDetfaultHeight: CGFloat = 20

class SEChatInquiryInfoHeader: UIView {
    
    var expandHandler: ((Bool) -> Void)?
    var tappedHandler: ((UIImageView?, String?) -> Void)?
    
    private var viewModel: SEChatViewModel? {
        didSet {
            collectionView.reloadData()
        }
    }
    
    func update(with vm: SEChatViewModel) {
        isHidden = false
        viewModel = vm
        expandBtn.isSelected = vm.isTableHeaderExpand
        collectionView.isHidden = !vm.isTableHeaderExpand
        descLabel.numberOfLines = vm.isTableHeaderExpand ? 0 : 1
        nameLabel.text = vm.inquiryInfo.patientName
        sexLabel.text = vm.patientSex
        ageLabel.text = vm.patientAge
        descLabel.text = vm.inquiryInfo.illness
        allergyLabel.text = vm.inquiryInfo.allergicContent
        pastLabel.text = vm.inquiryInfo.pastHistory
    }
    
    func relayoutUI(_ vm: SEChatViewModel) {
        descLabel.numberOfLines = vm.isTableHeaderExpand ? 0 : 1
        UIView.animate(withDuration: 0.25, delay: 0, options: .curveLinear) {
            self.collectionViewHeightConstraint.constant = vm.collectionViewHeight
            let transform = vm.isTableHeaderExpand ? CGAffineTransform(rotationAngle: -.pi) : .identity
            
            UIView.animate(withDuration: 0.25) {
                self.expandBtn.transform = transform
            }
        }
    }
    
    @IBAction private func expandAction() {
        expandBtn.isSelected.toggle()
        collectionView.isHidden = !expandBtn.isSelected
        expandHandler?(expandBtn.isSelected)
    }
    
    // MARK: - getter
    
    @IBOutlet var collectionViewHeightConstraint: NSLayoutConstraint! {
        didSet {
            collectionViewHeightConstraint.constant = 0
        }
    }
    
    @IBOutlet private weak var containerView: UIView! {
        didSet {
            containerView.setLayerCorners(rect: containerView.bounds, radius: 6)
            containerView.setShadow()
        }
    }
    
    @IBOutlet private weak var nameLabel: UILabel!
    @IBOutlet private weak var sexLabel: UILabel!
    @IBOutlet private weak var ageLabel: UILabel!
    @IBOutlet private weak var descLabel: UILabel!
    @IBOutlet private weak var allergyLabel: UILabel!
    @IBOutlet private weak var pastLabel: UILabel!
    @IBOutlet private weak var collectionView: UICollectionView! {
        didSet {
            collectionView.backgroundColor = .clear
            collectionView.delegate = self
            collectionView.dataSource = self
            collectionView.showsHorizontalScrollIndicator = false
            
            collectionView.register(SEChatInquiryInfoImageCell.nibForCell(),
                                    forCellWithReuseIdentifier: SEChatInquiryInfoImageCell.reuseIdentifier())
        }
    }
    @IBOutlet private weak var expandBtn: UIButton!
}

// MARK: - UICollectionViewDataSource

extension SEChatInquiryInfoHeader: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel?.numberOfItems ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SEChatInquiryInfoImageCell.reuseIdentifier(), for: indexPath) as! SEChatInquiryInfoImageCell
        cell.update(imageUrl: viewModel?.inquiryInfo.picList?[indexPath.item].shareThumbnailUrl)
        return cell
    }
}

// MARK: - UICollectionViewDelegate & UICollectionViewDelegateFlowLayout

extension SEChatInquiryInfoHeader: UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        let model = viewModel?.inquiryInfo.picList?[indexPath.item]
        let cell = collectionView.cellForItem(at: indexPath) as? SEChatInquiryInfoImageCell
        tappedHandler?(cell?.imageView, model?.shareUrl)
    }
    
    func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? SEChatInquiryInfoImageCell
        cell?.backgroundColor = .background
    }
    
    func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? SEChatInquiryInfoImageCell
        cell?.backgroundColor = .clear
    }
}
